package cn.edu.hznu.util.socket.tcp;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
	
	public static void main(String[] args) {
		try {
			ServerSocket serverSocket = new ServerSocket(3000);
			boolean f = true;
			while (f) {
				Socket client = serverSocket.accept();
				System.out.println("Successful accept!");
				new Thread(() -> {
					try {
						PrintStream out = new PrintStream(client.getOutputStream());
						BufferedReader bufFromClient = new BufferedReader(new InputStreamReader(client.getInputStream()));
						boolean flag = true;
						while (flag) {
							String str = bufFromClient.readLine();
							if (str == null || str.isEmpty()) {
								flag = false;
							} else {
								if (str.equals("bye")) {
									flag = false;
								} else {
									out.println(str);
								}
							}
						}
						
					} catch (Exception e) {
						e.printStackTrace();
					}
				}).start();
			}
			serverSocket.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
